var form = layui.form,id=getQueryVariable("id");

//表单初始值，修改时调用
if(id!=""){
    $("span")[0].innerHTML="修改角色组";
    var data=query(id);
    form.val("charactergroup-form",{
        "name":data.name,
        "code":data.code,
        "desc":data.desc,
    })
    $("input[name='code']")[0].readOnly=true;
    form.render();
}else{
    $("span")[0].innerHTML="新增角色组";
}

//监听提交
form.on('submit(charactergroup-form-element)', function(data){
    if(id!=""){
        if(update(data.field)){
            layer.msg("保存成功", {icon: 6});
            refresh();
        }else{
            layer.msg("保存失败", {icon: 6});
        }
    }else{
        if(isExit(data.field.name)){
            layer.msg("名称已存在！", {icon: 6});
            return false;
        }
        var num = insert(data.field);
        if(num <10000 && num >0){
            layer.msg("保存成功", {icon: 6});
            refresh();
        }else if(num <10000 && num ==0){
            layer.msg("本地保存失败", {icon: 6});
        }else if(num > 10000 ){
            layer.msg("同步云枢保存失败", {icon: 6});
        }
    }


    return false;
});
function refresh() {
    setTimeout(function () {
        //保存成功则刷新提交过来的界面 并关闭此页面
        parent.detab("views/jsp/SystemCenter/charactergroup.jsp");
        parent.intab("views/jsp/SystemCenter/charactergroup.jsp","角色组设置")
        parent.detab("views/jsp/SystemCenter/tpl/newupcharactergroup.jsp"+(id==''?'':'?id='+id));
    },1000)
}
function  query(id) {
    var obj={};
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"id":id},
        url:'rolegroup/selectById',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                obj=da.data;

            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return obj;
}
function insert(data) {
    var flag=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data),
        url:'rolegroup/insert',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                //flag=true;
                flag=da.data;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return flag;
}
function  update(data) {
    data.id=id;
    var flag=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data),
        url:'rolegroup/update',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                flag=true;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return flag;
}

function isExit(name) {
    var tf=false;
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"name":name},
        url:'rolegroup/queryByName',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf=da.data;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return tf;
}